package com.zingrow.web.configure.microuser.service.impl;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import jxl.Sheet;
import jxl.Workbook;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zingrow.common.utils.CommonUtils;
import com.zingrow.common.utils.PageView;
import com.zingrow.common.utils.StringUtil;
import com.zingrow.web.base.service.impl.BaseService;
import com.zingrow.web.common.utils.AnalyzeResult;
import com.zingrow.web.configure.microuser.mapper.MicrouserMapper;
import com.zingrow.web.configure.microuser.model.Microuser;
import com.zingrow.web.configure.microuser.service.IMicrouserService;
@Service
public class MicrouserService extends BaseService<Microuser> implements IMicrouserService {
	@Autowired
	private MicrouserMapper microuserMapper;
	@Override
	public void initMapper(){
		this.baseMapper = microuserMapper;
	}
	//模糊查询
	@Override
	public List<Microuser> queryParams(PageView pageView,String microuser_bloggerId,String microuser_blogger,
			String microuser_bloggerUrl,Integer is_exprot){
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("paging", pageView);
		map.put("microuser_bloggerId", microuser_bloggerId);
		map.put("microuser_blogger",microuser_blogger);
		map.put("microuser_bloggerUrl", microuser_bloggerUrl);
		map.put("is_exprot", is_exprot);
		return microuserMapper.queryParams(map);
	}
	//删除
	@Override
	public boolean deleteMicrouser(Integer microuser_id){
		return microuserMapper.deleteMicrouser(microuser_id) > 0 ;
	}
	//新增
	@Override
	public boolean addMicrouser(Microuser microuser){
		//给状态赋值为1
		if(microuser.getIs_delete()==null){
			microuser.setIs_delete(1);
		}
		//导出状态默认值为0
		if(microuser.getIs_exprot()==null){
			microuser.setIs_exprot(0);
		}
		return microuserMapper.addMicrouser(microuser) > 0;
	}
	//修改
	@Override
	public boolean modifyMicrouser(Microuser microuser){
		return microuserMapper.modifyMicrouser(microuser) > 0;
	}
	//修改显示
	@Override
	public boolean modifyDisplay(int display,List<String> displaylist){
		if(display==1)
		{
			return microuserMapper.updatadisplay(displaylist)>0;
		}
		else
		{
			return microuserMapper.updatadisplayno(displaylist)>0;
		}
		}
	//查询相同微博id
	@Override
	public boolean queryByid(Integer microuser_id,String microuser_bloggerUrl,String microuser_blogger,String microuser_bloggerId){
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("microuser_id", microuser_id);
		map.put("microuser_bloggerUrl", microuser_bloggerUrl);
		map.put("microuser_blogger", microuser_blogger);
		map.put("microuser_bloggerId", microuser_bloggerId);
		return microuserMapper.queryByid(map) > 0;
	}
	//单个查询
	@Override
	public Microuser queryById(Integer microuser_id){
		Microuser microuser = microuserMapper.queryById(microuser_id);
		return microuser;
	}
	//查询全部的接口
	@Override
	public List<Microuser> queryByIdName(PageView pageView){
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("pageing", pageView);
		return microuserMapper.queryByIdName(map);
	}
	//接口
		@Override
		public List<Microuser> queryMicrouserByIdList(List<String> idlist) {
			if(idlist != null){
				return microuserMapper.queryMicrouserByIdList(idlist);
			}
			return null;
		}

	//导出
	@Override
	public List<Microuser> queryMicrouserbyid(List<Integer> ids){
		List<Microuser> mlist = new ArrayList<Microuser>();
		mlist = microuserMapper.queryMicrouserbyid(ids);
			if(mlist.size()>0){
				microuserMapper.modifyMicrouserIsexprot(ids);
			}
		if(mlist != null && mlist.size() > 0){
			for (Microuser microuser : mlist) {
				if(microuser.getIs_delete()==0){
					microuser.setIs_deletes("停用");
				}else if(microuser.getIs_delete()==1){
					microuser.setIs_deletes("启用");
				}else{
					microuser.setIs_deletes("未知");
				}
			}
		}
		return mlist;
	}
    @Override
    public List<Microuser> queryMicroserByType(PageView pageView,String microuser_bloggerId, String microuser_blogger) {
            Map<String,Object> map = new HashMap<String,Object>();
            map.put("paging", pageView);
            map.put("microuser_bloggerId", microuser_bloggerId);
            map.put("microuser_blogger", microuser_blogger);
        return microuserMapper.queryMicroserByType(map);
    }
	
	
	//导入
	@Override
	public void analyzeExcelData(File file,AnalyzeResult<Microuser> result)throws Exception{
		List<Microuser> list = new ArrayList<Microuser>();
		//解析文件
		Workbook work = Workbook.getWorkbook(file);
		//获取第一个工作表
		Sheet sheet = work.getSheet(0);
		int cols = 0;
		int rows = 0;
		//判断工作表获取是否正常
		if(CommonUtils.isObjectNotNull(sheet)){
			//得到列的数量
			cols = sheet.getColumns();
			//得到行的数量
			rows = sheet.getRows();
			if(cols<=1||rows<=1){
				result.setResult(false, "未检测到数据", new ArrayList<Microuser>());
			}else if(rows>=9999999){
				result.setResult(false, "导入数据过多，请拆分成多次导入" + rows, new ArrayList<Microuser>());
			}
		}else{
			//可能是没有工作表
			result.setResult(false, "Excel工作表格，格式错误", new ArrayList<Microuser>());
		}if(!result.isSuccess()){
			//判断数据不合法直接返回
			return;
		}
		//获取正常进行下一个判断
		for (int i = 1; i < rows; i++) {
			for (int j = 0; j < cols; j++) {
				String microuser_bloggerId = sheet.getCell(j++, i).getContents().trim();
				String microuser_blogger = sheet.getCell(j++, i).getContents().trim();
				String microuser_bloggerUrl = sheet.getCell(j++, i).getContents().trim();
				//判断参数合法性
				if (microuser_bloggerId.length()<0||microuser_bloggerId.length()>50) {
					result.setResult(false, "第"+(i+1)+"行，格式错误", new ArrayList<Microuser>());
				}
				if (StringUtils.isBlank(microuser_bloggerId)) {
					result.setResult(false, "第"+(i+1)+"行，格式错误", new ArrayList<Microuser>());
				}
				if (microuser_blogger.length()<0||microuser_blogger.length()>50) {
					result.setResult(false, "第"+(i+1)+"行，格式错误", new ArrayList<Microuser>());
				}
				if (StringUtils.isBlank(microuser_blogger)){
					result.setResult(false, "第"+(i+1)+"行，格式错误", new ArrayList<Microuser>());
				}
				if (microuser_bloggerUrl.length()<0||microuser_bloggerUrl.length()>200) {
					result.setResult(false, "第"+(i+1)+"行，格式错误", new ArrayList<Microuser>());
				}
				if (StringUtils.isBlank(microuser_bloggerUrl)){
					result.setResult(false, "第"+(i+1)+"行，格式错误", new ArrayList<Microuser>());
				}
				//通过以上判断，添加到对象中
				if(result.isSuccess()){
					Microuser microuser = new Microuser(null, microuser_bloggerId, microuser_blogger, microuser_bloggerUrl);
					list.add(microuser);
				}else{
					//错误直接返回
					return;
				}
			}
		}
		result.setResult(true, null, list);
	}
}
